﻿  /**
	ArbolNegro - actionscript3 multipurpose code
    Copyright (C) 2010  Sebastián Sanabria Díaz - arbolnegro.absulit.net - arbolnegroaAS3@gmail.com - admin@absulit.net

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */

package net.absulit.arbolnegro.interfaces 
{
	import flash.display.DisplayObject;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	/**
	 * ...
	 * @author Sebastián Sanabria Díaz
	 */
	public class Container
	{
		private var _content:DisplayObject;
		private var _x:Number;
		private var _y:Number;
		private var _width:Number;
		private var _height:Number;
		private var _tweenX:Tween;
		private var _tweenY:Tween;
		private var _scaleState:String;
		private var _drag:Boolean;
		private var _scroll:Boolean;
		private var _centerV:Boolean;
		private var _centerH:Boolean
		public function Container() 
		{
			init();
		}
		
		
		private function init():void {
			_content = null;
			_x = 0;
			_y = 0;
			_width = 0;
			_height = 0;
			_tweenX = null;
			_tweenY = null;
			_scaleState = null;
			_drag = false;
			_scroll = false;
		}
		
		
		public function get scaleState():String { return _scaleState; }
		
		public function set scaleState(value:String):void 
		{
			_scaleState = value;
			if(_content != null){
				if (_scaleState == ContainerScaleStates.AUTO) {
					scaleAuto();
				}else
				if (_scaleState == ContainerScaleStates.WIDTH) {
					scaleWidth();
				}else
				if (_scaleState == ContainerScaleStates.HEIGHT) { 
					scaleHeight();
				}else
				if (_scaleState == ContainerScaleStates.AUTO_REVERSE) {
					scaleAutoReverse() ;
				}
			}
		}
		
		public function get width():Number { return _width; }
		
		public function set width(value:Number):void 
		{
			_width = value;
			scaleState = _scaleState;
		}
		
		public function get height():Number { return _height; }
		
		public function set height(value:Number):void 
		{
			_height = value;
			scaleState = _scaleState;
		}		
		
		public function get content():DisplayObject { return _content; }
		
		public function set content(value:DisplayObject):void 
		{
			_content = value;			
			_x = value.x;
			_y = value.y;
			scaleState = _scaleState;
		}
		
		public function get x():Number { return _x; }
		
		public function set x(value:Number):void 
		{
			_x = value;
			scaleState = _scaleState;
		}
		
		public function get y():Number { return _y; }
		
		public function set y(value:Number):void 
		{
			_y = value;
			scaleState = _scaleState;
		}
		
		public function get drag():Boolean { return _drag; }
		
		public function set drag(value:Boolean):void 
		{
			_drag = value;			
			if (_drag) {
				if (!_content.hasEventListener(MouseEvent.MOUSE_MOVE)) {
					_content.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveDrag);
					_content.addEventListener(MouseEvent.MOUSE_UP,onMouseUpDrag);
					_content.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownDrag);
					_content.addEventListener(MouseEvent.MOUSE_OUT, onMouseUpDrag);
				}
			}else 
			{
				if (_content.hasEventListener(MouseEvent.MOUSE_MOVE)) {
					_content.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveDrag);
					_content.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpDrag);
					_content.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownDrag);
					_content.removeEventListener(MouseEvent.MOUSE_OUT,onMouseUpDrag);
				}
			}			
		}
		
		public function get scroll():Boolean { return _scroll; }
		
		public function set scroll(value:Boolean):void 
		{
			_scroll = value;
			if (_scroll) {
				_content.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveScroll);
			}else {
				_content.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveScroll);
			}
		}
		
		private function onMouseMoveScroll(e:MouseEvent):void 
		{
			//TODO: version simplificada en SelectionStrip
			var enX:Boolean = (_content.parent.mouseX > this._x) && (_content.parent.mouseX < (this._x + this._width));
			var enY:Boolean = (_content.parent.mouseY > this._y) && (_content.parent.mouseY < (this._y + this._height));
			if (enX && enY) {
				var px = (_content.parent.mouseX - this._x) / this._width;
				var py = (_content.parent.mouseY - this._y) / this._height;
				var nx = this._x - (px * ((_content.width - this._width - 1)));
				var ny = this._y - (py * ((_content.height - this._height - 1)));
				///
				_tweenX = new Tween(_content, "x", Strong.easeOut, _content.x, nx, 1, true);
				_tweenY = new Tween(_content, "y", Strong.easeOut, _content.y, ny, 1, true);
			} else {
				
			}
		}
		
		private function onMouseMoveDrag(e:Event):void 
		{
			var enX:Boolean = (_content.parent.mouseX > this._x) && (_content.parent.mouseX  < (this._x + this._width));
			var enY:Boolean = (_content.parent.mouseY > this._y) && (_content.parent.mouseY < (this._y + this._height));
			if (!(enY && enX)) {
				Sprite(_content).stopDrag();
				Mouse.cursor = "arrow"
			} else {
				Mouse.cursor = "hand";
			}
			if (_content.x > this._x) {
				_content.x = this._x;
			}
			if (_content.y > this._y) {
				_content.y = this._y;
			}
			if ((_content.x + _content.width) < (this._x + this._width)) {
				_content.x = _x - (_content.width - this._width);
			}
			if ((_content.y + _content.height) < (this._y + this._height)) {
				_content.y = _y - (_content.height - this._height);
			}
		}
		
		private function onMouseDownDrag(e:Event):void 
		{
			var enX:Boolean = (_content.parent.mouseX > this._x) && (_content.parent.mouseX  < (this._x + this._width));
			var enY:Boolean = (_content.parent.mouseY > this._y) && (_content.parent.mouseY < (this._y + this._height));
			if (enY && enX) {
				Sprite(_content).startDrag();
			}
		}
		
		private function onMouseUpDrag(e:Event):void 
		{
			Sprite(_content).stopDrag();
		}
	
		private function scaleWidth():void {
			var ratio:Number = _content.width/_width
			_content.width /= ratio;
			_content.height /= ratio;		
		}
		
		private function scaleHeight() {
			var ratio:Number = _content.height / _height
			_content.width /= ratio;
			_content.height /= ratio;
		}
		
		private function scaleAuto():void {
			var containerRatio:Number = _width / _height;
			var contentRatio:Number = _content.width / _content.height
			var ratio  = containerRatio / contentRatio;
			if (ratio > 1) {
				scaleWidth();
			}else {
				scaleHeight();
			}
		}
		
		private function scaleAutoReverse():void
		{
			var containerRatio:Number = _width / _height;
			var contentRatio:Number = _content.width / _content.height
			var ratio  = containerRatio / contentRatio;
			if (ratio > 1) {
				scaleHeight();				
			}else {
				scaleWidth();
			}
		}
		
	}

}